1 Introduction

1.1 What is OSM?

OpenStreetMap (OSM) is an open-source platform that allows users to collaboratively edit and download geographic features with a standardised tagging structure. Each feature in OSM is described using a set of key-value tags. The key represents a general category of geographic elements, such as amenity, highway, water. The value represents a more specific classification within that category (key). For example, the key highway may include values like motorway, trunk, primary, and other road types. Therefore, this tagging system enables efficient structuring and extraction of geospatial features for elements across the globe.

See here for all the tags for key and value in OSM.

1.2 R package required

The package required for connecting to OSM is osmdata. Please install the package as follows and load it into R code.

install.packages("osmdata")
library(osmdata)

2 Using osmdata

2.1 Basic structure

The basic structure accessing OSM data through code is as follows. The BOUNDARY_BOX defines the spatial extent of the area from which we want to retrieve OSM data. Within the add_osm_feature() function, we need to specify the argument key and value to download the desired category of geographic features. Finally, using osmdata_sf() function to retrieve all the simple features (spatial data).

opq(BOUNDARY_BOX)%>%
  add_osm_feature(key="KEY", value="VALUE")%>%
  osmdata_sf()

The final results include points, lines, polygons, and also the multi-geometry.

2.2 Example

Now we take accessing the footpath data in Canberra for example.

First of all, read the polygon data to define our target area. And please make sure that the CRS is EPSG::4326, which aligns with the CRS for OSM. If the CRS is not consistent, please use function st_transform() to convert it.

canberra=read_sf("https://raw.githubusercontent.com/tonywr71/GeoJson-Data/refs/heads/master/suburb-2-act.geojson")

print(st_crs(canberra)) # EPSG:4283
## Coordinate Reference System:
##   User input: GDA94 
##   wkt:
## GEOGCRS["GDA94",
##     DATUM["Geocentric Datum of Australia 1994",
##         ELLIPSOID["GRS 1980",6378137,298.257222101,
##             LENGTHUNIT["metre",1]]],
##     PRIMEM["Greenwich",0,
##         ANGLEUNIT["degree",0.0174532925199433]],
##     CS[ellipsoidal,2],
##         AXIS["geodetic latitude (Lat)",north,
##             ORDER[1],
##             ANGLEUNIT["degree",0.0174532925199433]],
##         AXIS["geodetic longitude (Lon)",east,
##             ORDER[2],
##             ANGLEUNIT["degree",0.0174532925199433]],
##     ID["EPSG",4283]]
canberra=st_transform(canberra, crs=4326)

Then retrieve the boundary for the area using function st_bbox().

# Extract the boundary of the spatial data
canberra_box=st_bbox(canberra)

print(canberra_box)
##      xmin      ymin      xmax      ymax 
## 148.76279 -35.92021 149.39929 -35.12442

Let’s plot a map to show the target area and its extent.

# Extract the boundary of the spatial data
canberra_box=st_bbox(canberra)

We can then access OSM data using the basic structure mentioned above.

# Take sidewalk in Canberra for example
sidewalk=opq(canberra_box)%>%
  add_osm_feature(key="footway", value="sidewalk")%>%
  osmdata_sf()

Finally, plot all the spatial data to see the results.

# Plot the result
tm_shape(canberra)+
  tm_polygons(alpha=0.2, border.alpha=0.2)+
  tm_shape(sidewalk$osm_lines)+
  tm_lines(col="blue")+
  tm_shape(st_as_sfc(canberra_box))+
  tm_borders(lty="dashed", col="red")